package com.heima.wemedia.controller.v1;

import com.heima.apis.wemedia.WmNewsControllerApi;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.media.dto.NewsDto;
import com.heima.model.media.dto.WmNewsDto;
import com.heima.model.media.dto.WmNewsPageReqDto;
import com.heima.model.media.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsControll implements WmNewsControllerApi {

   @Autowired
    WmNewsService wmNewsService;

    /**
     * 分页条件查询所有
     * @param wmNewsPageReqDto
     * @return
     */
    @Override
    @PostMapping("/list")
    public ResponseResult<List<WmNews>> findAll(@RequestBody WmNewsPageReqDto wmNewsPageReqDto) {


        return wmNewsService.findAll(wmNewsPageReqDto);
    }

    /**
     * 查询状态为8的所有待发布文章的id，进行定时发布
     * @param
     * @return
     */
    @Override
    @GetMapping("/findAllByStatus")
    public ResponseResult<List<Integer>> findAllByStatus() {
        return wmNewsService.findAllByStatus();
    }

    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findById/{id}")
    public ResponseResult<WmNews> get(@PathVariable("id") Integer id) {
        WmNews news = wmNewsService.getById(id);
        return ResponseResult.okResult(news);
    }

    @Override
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {

        return wmNewsService.delete(id);
    }


    /**
     * 自媒体文章-发布、修改，保存草稿
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {

        return wmNewsService.submitNews(dto);
    }



    /**
     * 文章上架和下架
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/downOrUp")
    public ResponseResult downOrUp(@RequestBody NewsDto dto) {

        return wmNewsService.downOrUp(dto);
    }

    @Override
    @PostMapping("/update")
    public ResponseResult updateWmNews(WmNews wmNews) {
      wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }



}
